package com.yunatt.service.impl;

import com.yunatt.mapper.AccessDayMapper;
import com.yunatt.mapper.DeviceMapper;
import com.yunatt.mapper.MachineCommandMapper;
import com.yunatt.model.AccessDayEntity;
import com.yunatt.model.DeviceEntity;
import com.yunatt.model.MachineCommandEntity;
import com.yunatt.service.AccessDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AccessDayServiceImpl implements AccessDayService {


    @Autowired
    private AccessDayMapper accessDayMapper;

    @Autowired
    private MachineCommandMapper machineCommandMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return accessDayMapper.deleteById(id);
    }

    @Override
    public int insert(AccessDayEntity record) {
        // TODO Auto-generated method stub
        return accessDayMapper.insert(record);
    }

    @Override
    public int insertSelective(AccessDayEntity record) {
        // TODO Auto-generated method stub
        return accessDayMapper.insert(record);
    }

    @Override
    public AccessDayEntity selectByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return accessDayMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(AccessDayEntity record) {
        // TODO Auto-generated method stub
        return accessDayMapper.updateById(record);
    }

    @Override
    public int updateByPrimaryKey(AccessDayEntity record) {
        // TODO Auto-generated method stub
        return accessDayMapper.updateById(record);
    }
    @Override
    public List<AccessDayEntity> selectAll() {
        // TODO Auto-generated method stub
        return accessDayMapper.selectAll();
    }

    @Override
    public void setAccessDay() {
        // TODO Auto-generated method stub
        StringBuilder sb=new StringBuilder();
        sb.append("{\"cmd\":\"setdevlock\",\"dayzone\":[");
        List<AccessDayEntity>accessDays=accessDayMapper.selectAll();
        List<AccessDayEntity>accessDaysTemp=new ArrayList<AccessDayEntity>();


        if(accessDays.size()<8){
            accessDaysTemp.addAll(accessDays);
            for (int i = accessDays.size(); i < 8; i++) {
                AccessDayEntity accessDay1=new AccessDayEntity();
                accessDay1.setId(i+1);

                accessDay1.setStartTime1("00:00");
                accessDay1.setEndTime1("00:00");
                accessDay1.setStartTime2("00:00");
                accessDay1.setEndTime2("00:00");
                accessDay1.setStartTime3("00:00");
                accessDay1.setEndTime3("00:00");
                accessDay1.setStartTime4("00:00");
                accessDay1.setEndTime4("00:00");
                accessDay1.setStartTime5("00:00");
                accessDay1.setEndTime5("00:00");
                accessDaysTemp.add(i, accessDay1);

            }

        }
        System.out.println(accessDaysTemp.size());
        for (int i = 0; i < accessDaysTemp.size(); i++) {
            System.out.println(accessDaysTemp.get(i));

        }

        for (int i = 0; i < accessDaysTemp.size(); i++) {
            sb.append("{\"day\":[");
            sb.append("{\"section\":\"" + accessDaysTemp.get(i).getStartTime1() + "~" + accessDaysTemp.get(i).getEndTime1() + "\"},");
            sb.append("{\"section\":\"" + accessDaysTemp.get(i).getStartTime2() + "~" + accessDaysTemp.get(i).getEndTime2() + "\"},");
            sb.append("{\"section\":\"" + accessDaysTemp.get(i).getStartTime3() + "~" + accessDaysTemp.get(i).getEndTime3() + "\"},");
            sb.append("{\"section\":\"" + accessDaysTemp.get(i).getStartTime4() + "~" + accessDaysTemp.get(i).getEndTime4() + "\"},");
            sb.append("{\"section\":\"" + accessDaysTemp.get(i).getStartTime5() + "~" + accessDaysTemp.get(i).getEndTime5() + "\"}");
            if (i != 7) {
                sb.append("]},");
            } else {
                sb.append("]}");
            }
        }
        sb.append("]}");

        // System.out.println("jshshhs"+sb.toString());
        String message=sb.toString();
        //System.out.println(message);
        //WebSocketPool.sendMessageToAllDeviceFree(message);
        List<DeviceEntity>deviceList=deviceMapper.findAllDevice();
        for (int i = 0; i < deviceList.size(); i++) {

            MachineCommandEntity machineCommand=new MachineCommandEntity();
            machineCommand.setContent(message);
            machineCommand.setName("setdevlock");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceList.get(i).getSerialNum());
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());

            //	machineComandService.addMachineCommand(machineCommand);
            machineCommandMapper.insert(machineCommand);
        }

    }
}
